<?php
session_start();
include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';
include 'inc/form_op.php';

date_default_timezone_set('PRC');

$page_title = "Project";
$debug = 0;
$new_project_field=array("dw_no", "wbs_no", "project_name");
perform_insert_post("submit_new_project", "project_info", $new_project_field);

function gen_new_project_form() {
  return '<p>创建新合同信息</p><p>
  <form method=post>
  DW号：<input type=text name=dw_no required />
  WBS号：<input type=text name=wbs_no reqired />
  合同名称：<input type=text name=project_name size=40 required />
  <button type=submit name=submit_new_project>提交</button>
  </form>
  </p>';
}

function get_project_info_maintainer($project_id) {
  $rows = db_get("select name from user_info where id in (select maintainer_id from project_info where id=$project_id)");
  if(1 == count($rows)) {
    $row = $rows[0];
    return $row["name"];
  } else {
    return "";
  }
}

function get_siteservice_before_2012($project_id) {
  $html_str = '<center><form method="post" name="del_service_rec" id="del_service_rec">
               <input type="hidden" name="del_service_id" id="del_service_id"></input>
               <input type="hidden" name="project_id" id="project_id" value="'.$_POST['project_id'].'"> </input>
               </form>

               <script language="JavaScript">
               function del_service_rec(service_id)
               {
                 if(confirm("真的要删除吗？")) {
                 document.del_service_rec.del_service_id.value=service_id;
                 document.del_service_rec.submit();
                 }
               }
               </script>';

  $sql = "select service_rec.id id, name, start,end,abstract,days from service_rec, user_info ".
         "where project_id=$project_id and user_id=user_info.id order by start";
  $rows = db_get($sql);
  $num = count($rows);

  $switch_code = 'javascript:switch_disp("work_log", "before_2012");';
  $switch_link = "<a href='$switch_code';>返回</a>";
  $html_str .= "<h2>2011年及以前现场服务记录</h2>$switch_link";

  if($num == 0) {
    $html_str .=  '<p>没有找到记录。</p>';
  } else {
    $total_days = 0;

    $html_str .=  '<table border=1>';
    $html_str .=  '<tr><th>服务人</th><th>开始时间</th><th>结束时间</th><th style="width:220px;">工作内容</th><th>天数</th><th>操作</th></tr>';

    foreach($rows as $row) {
      $total_days += $row['days'];
      $show_name=$row['name'];
      $op_str='-';
      if(0==strcmp($show_name, $_SESSION['name'])) {
        $show_name='我';
        $op_str='<a href= "javascript:del_service_rec('.$row['id'].');">删除</a>';
      }

    $html_str .=  "<tr><td align='middle'>$show_name</td><td>".$row['start']."</td><td>".$row['end']."</td>";
    $html_str .=  '<td  style="width:260px;">'.$row['abstract'].'</td><td align="right">'.$row['days']."</td>";
    $html_str .=  '<td align="center">'.$op_str.'</td></tr>';
    }
    $html_str .=  "</table>";
    $html_str .=  "<p>累计服务人天：$total_days</p>";
  }


  $html_str .=  '<fieldset style="width:400px;padding:10px;">'.
       '<legend>新增：</legend>'.
       '<table>'.
       '<form method="post" name="add_service_rec" id="add_service_rec">'.
       '<input type="hidden" name="project_id" id="project_id" value="'.$_POST['project_id'].'" />'.
       '<tr><td align=right>我从：</td><td><input type=date value="'.date('Y-m-d',strtotime("-3 day")).'" required name="start" id="start"/></td></tr>'.
       '<tr><td align=right>到：</td><td><input type=date value="'.date('Y-m-d').'" required name="end" id="end"/></td></tr>'.
       '<tr><td align=right>在此天数：</td><td><input type="number" value="2" required name="days"/></td></tr>'.
       '<tr><td align=right>工作内容：</td><td><textarea required name="abstract" id="abstract" rows="4" cols="26" placeholder="可最多输入128汉字。"></textarea></td></tr>'.
       '<tr><td></td><td align="right"><button type="submit" name="submit_service_rec" id="submit_service_rec">增加</button></td></tr>'.
       '</form></table></fieldset>';
  $html_str .= "</center>";
  return $html_str;
}

function get_wlog_table($project_id) {
  $switch_code = 'javascript:switch_disp( "before_2012", "work_log");';
  $switch_link = "<a href='$switch_code';>查看2011年及以前现场服务记录</a>";
  $html_str = "<center><h2>工作记录</h2>$switch_link</center>";

  $sql = "SELECT DATE( work_log.end_time ) work_date, user_info.name user_name, task_info.title task_title, ".
         "task_info.content task_content, work_log.title work_title, work_log.content work_content, work_log.work_hours ".
         "FROM work_log ".
         "JOIN task_info ".
         "JOIN user_info ".
         "JOIN project_info ON work_log.task_id = task_info.id ".
         "AND work_log.user_id = user_info.id ".
         "AND task_info.project_id = project_info.id ".
         "AND project_info.id = $project_id ".
         "order by work_date desc";
  //echo '$sql = ', $sql;
  $rows = db_get($sql);
  $table_fields = array("work_date"=>"&nbsp日&nbsp&nbsp期&nbsp", "user_name"=>"姓&nbsp&nbsp名",// "task_content"=>"任  务", 
      "work_content"=>"工   作   内   容", "work_hours"=>"耗时");

  $html_str .= gen_table($table_fields, $rows);
  
  if(count($rows) > 1) {
    $sql = "select sum(work_hours) sum_hours from work_log join task_info on work_log.task_id=task_info.id and task_info.project_id = $project_id";
    $sums = db_get($sql);
    if(count($sums) > 0) {
      $sum = $sums[0];
      $sum_hours = $sum['sum_hours'];
      $html_str .= "累计工作时间：$sum_hours"."人时。";
    }
  }
  
  return $html_str;
}

function get_switchable_rows($field_name, $descrip_name, $field_value) {
  $val_row_id = $field_name.'_val_row';
  $modify_row_id = $field_name.'_modify_row';
  $form_name='modify_'.$field_name;
  $textarea_name_suffix = "_value";
  $textarea_name=$field_name.$textarea_name_suffix;
  $switch_modify_code="javascript:switch_disp('".$modify_row_id."','".$val_row_id."');";
  $switch_val_code="javascript:switch_disp('".$val_row_id."','".$modify_row_id."');";
  $html_str .=  '<tr id="'.$val_row_id.'"><td align="right"><em>'.$descrip_name.'：</em></td><td>';
  $html_str .=  str_replace(chr(13), '<br/>', $field_value);
  $html_str .=  '</td><td><a href="'.$switch_modify_code.'">维护</a></td></tr>';
  $html_str .=  '<tr id='.$modify_row_id.' style="display:none"><form method=POST><input type="hidden" name="project_id" id="project_id" value="'.$_POST['project_id'].'"/><td align="right">'.$descrip_name;
  $html_str .=  '：</td><td><textarea name = "'.$textarea_name.'" required rows=10 cols=46>'.$field_value.
    '</textarea></td><td><button type="submit">修改</button><br/><a href='.$switch_val_code.'>取消</a></td></form></tr>';
  return $html_str;
}
  
function gen_file_rows($file_type, $type_desc, $single) {
  echo '<tr><td><em>'.$type_desc.'：</em></td><td>';
  if ($single) {
    $existing=false;
    //get $existing
    if($existing) {
      echo '<a href="#">发送到邮箱</a>';
    } else {
      echo '尚无文件，<a href="#">上传</a>或';
      $subscribed = false;
      //get $subscribed
      if(!$subscribed) {
        echo '<a href="#">订阅</a>';
      } else {
        echo '<a href="#">取消订阅</a>';
      }
    }
  } else {
    $files=Array("");
    //get files
    foreach($files as $file) {
      echo '<a href="#">文件x</a><br/>';
    }
    echo '<a href="#">添加</a>';
  }
  echo '</td><td></td></tr>';
}

function get_project_id() {
  $project_id = -1;
  if (isset($_POST['project_id'])) {
    $project_id = $_POST['project_id'];
  } else if(isset($_POST['submit_new_project']) && isset($_POST['dw_no'])) {
    $rows = db_get("select id from project_info where dw_no = '".$_POST['dw_no']."'");
    if(count($rows) != 0) {
      $project_id = $rows[0]["id"];
    }
  }
  return $project_id;
}

function get_project_info_html($project_id) {
  $html_str .=  '<center><h2>项目信息</h2>';
  $html_str .=  '<p>信息维护责任人：'.get_project_info_maintainer($project_id).'</p>';

  $modifiable_fields = Array("modules"=>"高软模块和状态", "contacts"=>"联系人信息", "configuration"=>"系统配置信息", "life_info"=>"生活信息", "customer_location"=>"地址");
  $textarea_name_suffix = "_value";
  $where = " where id = ".$project_id;
  foreach($modifiable_fields as $field=>$descrip) {
    $textarea_name = $field.$textarea_name_suffix;
    //echo 'field:'.$textarea_name.'</br>';
    if(isset($_POST[$textarea_name])) {
      $backup_sql = "insert into project_info_bak(id, ".$field.", bak_user_name) select id, ".$field.", '".$_SESSION['name']."' from project_info".$where;
      $update_sql = "update project_info set ".$field." = '".$_POST[$textarea_name]."' ".$where;
      //echo $backup_sql.'</br>';
      //echo $update_sql.'</br>';
      mysql_query($backup_sql);
      mysql_query($update_sql);
    }
  }

  $sql = "select dw_no, wbs_no, project_name, committed, modules, contacts, configuration, life_info, customer_location from project_info where id =".$project_id;
  $rows = db_get($sql);
  $num = count($rows);
  //echo $num;
  if($num > 0) {
    $row=$rows[0];

    $html_str .=  '<table border=1>'.
    '<tr><td align="right"><em>项目名称：</em></td><td style="width:300px;">'.$row['project_name'].'</td><td></td></tr>'.
    '<tr><td align="right"><em>合同号：</em></td><td>'.$row['dw_no'].'</td><td></td></tr>'.
    '<tr><td align="right"><em>WBS号：</em></td><td>'.$row['wbs_no'].'</td><td></td></tr>'.
    '<tr><td align="right"><em>投运状态：</em></td><td>'; 
    if($row['committed'])$html_str .=  '已投运';else $html_str .=   '未投运';
    $html_str .=  '</td><td></td></tr>';

    foreach($modifiable_fields as $field=>$descrip) {
      $html_str .= get_switchable_rows($field, $descrip, $row[$field]);
    }
    $html_str .= "</table>";
  }
  $html_str .= "</center>";
  return $html_str;
}

if(isset($_POST['del_service_id'])) {
  $back_sentence = "insert into service_rec_bak(user_id, project_id, start, end, abstract, days, bak_user_name) select user_id, project_id, start, end, abstract, days, '".$_SESSION['name'];
  $back_sentence .= "' from service_rec where id = ".$_POST["del_service_id"];
  //echo $back_sentence."\n";
  mysql_query($back_sentence);
  $del_sentence = "delete from service_rec where id=".$_POST["del_service_id"];
  //echo $del_sentence."\n";
  mysql_query($del_sentence);
}
  
if(isset($_POST['submit_service_rec'])) {
  $sql_sentence = "insert into service_rec(user_id, project_id, start, end, abstract, days) select id, '";
  $sql_sentence .= $project_id."', '".$_POST["start"]."', '".$_POST["end"]."', '".$_POST["abstract"]."', ".$_POST["days"];
  $sql_sentence .= " from user_info where name like '".$_SESSION["name"]."'";
  mysql_query($sql_sentence);
}

if (!isset($_POST['project_id'])&&!isset($_POST['submit_new_project'])) {
  $content = isset($_SESSION['user_id']) && (is_administrator($_SESSION['user_id']) ||is_manager($_SESSION['user_id'])) ? gen_new_project_form() : '<p>未选择项目</p>';
  echo gen_onecol_page("Project", $content, $GLOBALS['debug']);
} else {
  $project_id = get_project_id();
  
  $left_content = get_project_info_html($project_id);
  $right_content = gen_switch_divs(array("before_2012"=>get_siteservice_before_2012($project_id), 
                                           "work_log"=>get_wlog_table($project_id)), 
                                           "work_log");

  echo gen_twincol_page($page_title, $left_content, $right_content, $GLOBALS['debug']);
}

?>